package com.ice.body.hardware.controller;
import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.WareHouseService;
import com.ice.body.hardware.model.DeviceMachine;
import com.ice.body.hardware.service.DeviceMachineService;
import io.swagger.annotations.ApiOperation;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-12-07
 * Time: 01:38
 */
@RestController
@ApiIgnore(value = "一体机")
public class DeviceMachineController extends BaseController{
    //---------------------------一体机-----------------------start
    @Resource
    private DeviceMachineService deviceMachineService;
    @Resource
    private WareHouseService wareHouseService;
    @ApiOperation(value = "一体机-新增")
    @PostMapping("/api/DeviceMachine/insert")
    public AjaxResult<DeviceMachine> insert_DeviceMachine(DeviceMachine model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            model.setCreateTime(new Date());
            deviceMachineService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "一体机-删除")
    @PostMapping("/api/DeviceMachine/deleteById")
    public AjaxResult<DeviceMachine> deleteById_DeviceMachine(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            deviceMachineService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "一体机-更新")
    @PostMapping("/api/DeviceMachine/updateById")
    public AjaxResult<DeviceMachine> update_DeviceMachine(DeviceMachine model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            deviceMachineService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "一体机-查询一个")
    @PostMapping("/api/DeviceMachine/selectById")
    public AjaxResult<DeviceMachine> selectById_DeviceMachine(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           DeviceMachine model = deviceMachineService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "一体机-查询多个")
    @PostMapping("/api/DeviceMachine/selectAll")
    public AjaxResult<DeviceMachine> selectList_DeviceMachine(DeviceMachine model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<DeviceMachine> list = deviceMachineService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "一体机-查询分页")
    @PostMapping("/api/DeviceMachine/selectListByPageHelper")
    public AjaxResult<DeviceMachine> selectListByPageHelper_DeviceMachine(@RequestParam(name = "pageSize") Integer pageSize,
                                                                          @RequestParam(name = "pageNum") Integer pageNum,
                                                                          DeviceMachine model){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           list = deviceMachineService.selectListByModel(model);
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------一体机-----------------------end
}
